package com.example.project.action;

import org.springframework.core.NamedThreadLocal;

import java.util.Date;
import java.util.HashMap;


/**
 * 用于存放分页参数、用户信息
 * 采用ThreadLocal是为了使每个线程保存一份属于自己的数据。
 * @author chenth
 * */
public final class ActionUtil {

	private static final ThreadLocal<ActionParam> ActionParamHolder = new NamedThreadLocal<>(
			"ActionParam Holder");

	private ActionUtil() {
		throw new IllegalStateException("Utility class");
	}




	/**
	 * 设置线程参数
	 * @param p
	 */
	public static void setActionParam(ActionParam p){
		ActionParamHolder.set(p);
	}
	
	public static ActionParam getActionParam(){
		return ActionParamHolder.get()==null?new ActionParam():ActionParamHolder.get();
	}
	
	public static int getStart(){
		return getActionParam().getStart();
	}
	
	public static int getLimit(){
		return getActionParam().getLimit();
	}

	
	public static boolean isBootStrapTable(){
		return getActionParam().getParam().get("offset")!=null;
	}
	
	public static boolean exist(){
		return getActionParam() != null;
	}

	/**
	 * 获取系统时间
	 * @return
	 */
	public static Date getSysTime() {
		return getActionParam().getSysTime();
	}

	
	/**
	 * 获取传递过来的某个参数的值
	 * @return
	 */
	public static String getParameter(String key) {
		return getActionParam().getParam().get(key);
	}
	
	/**
	 * 获取参数Map
	 * @return
	 */
	public static HashMap<String, String> getParameterMap() {
		return ActionUtil.getActionParam().getParam();
	}
}
